package com.nbgroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbgroup.mapper.StatementMapper;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.common.PageResult;
import com.nbgroup.pojo.dto.*;
import com.nbgroup.pojo.vo.*;
import com.nbgroup.service.StatementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StatementServiceImpl implements StatementService {
    @Autowired
    private StatementMapper statementMapper;

    //客户报表
    @Override
    public ClientStatementVo clientStatement(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        // 查询客户声明信息
        // 从数据库中获取指定日期范围内的客户声明消息
        List<ClientStatementMessagesVo> MessagesVo = statementMapper.clientStatement(begin, end);
        log.info("sql返回的数据{}", MessagesVo);
        // 提取日期列表，用于图表的X轴
        List<LocalDate> date = MessagesVo.stream().map(ClientStatementMessagesVo::getDate).toList();
        List<Integer> newList = List.of();
        if (MessagesVo.get(1) != null) {
            // 提取新增客户数列表，用于图表的数据序列之一
            newList = MessagesVo.stream().map(ClientStatementMessagesVo::getNewCustomer).toList();
        }
        List<Integer> totalList = List.of();
        if (MessagesVo.get(2) != null) {
            // 提取总客户数列表，用于图表的另一个数据序列
            totalList = MessagesVo.stream().map(ClientStatementMessagesVo::getTotalCustomer).toList();
        }


        // 创建一个客户声明视图对象，用于封装图表数据
        ClientStatementVo clientStatementVo = new ClientStatementVo();

        // 设置图表的X轴数据为日期列表
        clientStatementVo.setXAxis(date);

        // 创建一个数组列表来存储图表的数据序列
        ArrayList<ClientStatementMessageVo> arrayList = new ArrayList<>();

        // 创建并配置新增客户数的数据序列
        ClientStatementMessageVo messageVo = new ClientStatementMessageVo();
        messageVo.setName("新增客户数");
        messageVo.setData(newList);

        // 创建并配置总客户数的数据序列
        ClientStatementMessageVo messageVo1 = new ClientStatementMessageVo();
        messageVo1.setName("总客户数");
        messageVo1.setData(totalList);

        // 将两个数据序列添加到图表的数据序列列表中
        arrayList.add(messageVo);
        arrayList.add(messageVo1);

        // 设置图表的数据序列列表
        clientStatementVo.setSeries(arrayList);

        // 返回封装好的客户声明视图对象
        return clientStatementVo;

    }

    // 销售统计报表
    @Override
    public ClientStatementVo salesStatistics(LocalDate begin, LocalDate end) {
        // 查询销售统计数据
        List<SalesStatisticsMessageVo> MessagesVo = statementMapper.salesStatistics(begin, end);
        // 提取日期和金额数据
        List<LocalDate> date = MessagesVo.stream().map(SalesStatisticsMessageVo::getDate).toList();
        List<Double> amount = MessagesVo.stream().map(SalesStatisticsMessageVo::getAmount).toList();
        // 创建ClientStatementVo对象并设置数据
        ClientStatementVo clientStatementVo = new ClientStatementVo();
        clientStatementVo.setXAxis(date);
        // 创建并配置ClientStatementMessageVo对象
        ClientStatementMessageVo messageVo = new ClientStatementMessageVo();
        messageVo.setName("销售统计");
        messageVo.setData(amount);
        // 将ClientStatementMessageVo对象添加到列表中
        ArrayList<ClientStatementMessageVo> arrayList = new ArrayList<>();
        arrayList.add(messageVo);
        // 设置系列数据并返回ClientStatementVo对象
        clientStatementVo.setSeries(arrayList);
        return clientStatementVo;
    }

    @Override
    public ClientStatementVo cluesStatistics(LocalDate begin, LocalDate end) {
        // 根据开始和结束日期获取线索统计信息
        List<ClueStatementMessagesVo> MessagesVo = statementMapper.cluesStatistics(begin, end);
        // 提取日期、新增线索和总线索数据
        List<LocalDate> dates = MessagesVo.stream().map(ClueStatementMessagesVo::getDate).toList();
        List<Integer> newClues = MessagesVo.stream().map(ClueStatementMessagesVo::getNewClues).toList();
        List<Integer> totalClues = MessagesVo.stream().map(ClueStatementMessagesVo::getTotalClues).toList();
        // 创建客户端声明视图对象并设置X轴数据为日期
        ClientStatementVo clientStatementVo = new ClientStatementVo();
        clientStatementVo.setXAxis(dates);
        // 创建并配置新增线索的消息视图
        ClientStatementMessageVo messageVo = new ClientStatementMessageVo();
        messageVo.setName("新增线索");
        messageVo.setData(newClues);
        // 创建并配置总线索的消息视图
        ClientStatementMessageVo messageVo1 = new ClientStatementMessageVo();
        messageVo1.setName("总线索");
        messageVo1.setData(totalClues);
        // 将新增线索和总线索的消息视图添加到系列列表中
        ArrayList<ClientStatementMessageVo> arrayList = new ArrayList<>();
        arrayList.add(messageVo);
        arrayList.add(messageVo1);
        // 设置系列列表到客户端声明视图中
        clientStatementVo.setSeries(arrayList);
        // 返回配置好的客户端声明视图对象
        return clientStatementVo;
    }

    //线索
    @Override
    public CluePercentVo getVulnerabilityMap(LocalDate begin, LocalDate end) {
        CluePercentVo cluePercentVo = statementMapper.getVulnerabilityMap(begin, end);
        return cluePercentVo;
    }

    //渠道统计
    @Override
    public List<ChanelStatisticsVo> chanelStatistics(LocalDate begin, LocalDate end) {
        return statementMapper.chanelStatistics(begin, end);
    }

    //活动统计
    @Override
    public List<ActivityStatisticsVo> activityStatistics(LocalDate begin, LocalDate end) {
        return statementMapper.activityStatistics(begin, end);
    }

    //学科统计
    @Override
    public List<SubjectStatisticsVo> subjectStatistics(LocalDate begin, LocalDate end) {

        return statementMapper.subjectStatistics(begin, end);
    }

    /**
     * 部门渠道统计
     */
    @Override
    public PageResult queryCon(DeptListDto dto, LocalDate begin, LocalDate end) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<DeptVo> list = statementMapper.queryDeptList(dto, begin, end);
        if (list == null) {
            return new PageResult(400, "部门渠道统计失败", null, null, null);
        }
        Page<DeptVo> p = (Page<DeptVo>) list;
        return new PageResult(200, "部门渠道统计", p.getTotal(), p.getResult(), null);
    }

    /***
     * 渠道统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageResult queryChan(DeptListDto dto, LocalDate begin, LocalDate end) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<ChanVo> list = statementMapper.queryChan(dto, begin, end);
        if (list == null) {
            return new PageResult(400, "部门渠道统计失败", null, null, null);
        }
        Page<ChanVo> p = (Page<ChanVo>) list;
        return new PageResult(200, "部门渠道统计", p.getTotal(), p.getResult(), null);
    }

    /**
     * 归属人
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageResult queryPeople(DeptListDto dto, LocalDate begin, LocalDate end) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<OwnerShipVo> list = statementMapper.queryPeople(dto, begin, end);
        if (list == null) {
            return new PageResult(400, "部门渠道统计失败", null, null, null);
        }
        Page<OwnerShipVo> p = (Page<OwnerShipVo>) list;
        return new PageResult(200, "部门渠道统计", p.getTotal(), p.getResult(), null);
    }

    //渠道统计list
    @Override
    public PageResult activityStatisticsList(PageActivityStatisticsListDTO pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        List<ActivityListvo> list = statementMapper.activityStatisticsList(pageDto.getCode(), pageDto.getName(), pageDto.getTotal());

        if (list == null) {
            return new PageResult(400, "活动渠道统计失败", null, null, null);
        }
        Page<ActivityListvo> p = (Page<ActivityListvo>) list;
        return new PageResult(200, "活动渠道统计", p.getTotal(), p.getResult(), null);

    }

    @Override
    public PageBean cluesStatisticsList(PageCluesStatisticsDTO pageDto) {
        pageDto.check();
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        Page<CluesStatisticsVo> page = statementMapper.cluesStatisticsList(pageDto);
        PageBean pageBean = new PageBean();
        pageBean.setRows(page.getResult());
        pageBean.setTotal(page.getTotal());
        return pageBean;
    }

    @Override
    public PageBean pageQuery(PageContractStatisticsDTO pageDto) {
        pageDto.check();
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        Page<ContractStatisticsListVO> page = statementMapper.pageQuery(pageDto);
        PageBean pageBean = new PageBean();
        pageBean.setRows(page.getResult());
        pageBean.setTotal(page.getTotal());
        return pageBean;
    }


}